<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FileAction
 *
 * @author Administrator
 */
import('@.Service.FileService');
import("ORG.Net.UploadFile");
import("@.ORG.Net.Keditor");

class FileAction extends Action {

    function upload() {
        // 实例化上传类
        $upload = new UploadFile();

        // 设置附件上传目录
//        $upload->savePath = './Public/images/';
        $upload->savePath = './App/Tpl/default/Public/images/upload/';

        $upload->saveRule = 'time';

        $upload->thumb = true;
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '175';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '175';

        if (!$upload->upload()) { // 上传错误提示错误信息
            $this->ajaxReturn($upload->getErrorMsg());
        } else { // 上传成功获取上传文件信息
            //$saeStorage = new SaeStorage();
            $info = $upload->getUploadFileInfo();

            $fileService = new FileService();
            $data['name'] = $info[0]['name'];
            //$data['url'] = $saeStorage->getUrl('images', $info[0]['savename']);
//            $data['thumb_url'] = $saeStorage->getUrl('images', 'thumb_' . $info[0]['savename']);

            $data['path'] = '../Public/images/upload/' . $info[0]['savename'];
            $data['thumb_url'] = '../Public/images/upload/' . 'thumb_' . $info[0]['savename'];
            //$data['photo_folder_id'] = 1;
            //$result = $fileService->insert($data);
            //返回缩略图路径



            $this->ajaxReturn($data, '上传成功', 1);
        }
    }

    function kindeditorUpload() {
        $ke = new Keditor();
        $ke->upload();
        
//        dump(1);
    }

    //上传更新网站背景
    function uploadBackGround() {
        // 实例化上传类
        $upload = new UploadFile();

        // 设置附件上传目录
//        $upload->savePath = './Public/images/';
        $upload->savePath = './App/Tpl/default/Public/images/';

        $headerBg = $upload->savePath . 'header-bg.jpg';
        $upload->saveRule = 'com_create_guid';
        if (!$upload->upload()) { // 上传错误提示错误信息
            $this->ajaxReturn($upload->getErrorMsg());
        } else { // 上传成功获取上传文件信息
            $info = $upload->getUploadFileInfo();

            $fileService = new FileService();
            $fileName = $info[0]['savename'];

            //删除原文件
            if (file_exists($headerBg)) {
                unlink($headerBg);
            }

            $renameResult = rename($upload->savePath . $fileName, $upload->savePath . 'header-bg.jpg');

            $this->ajaxReturn($fileName, $renameResult);
        }
    }

}

?>
